import numpy as np
import math

def power_method(transition_matrix, d, e, n):
    #选择初始向量x0
    x0 = np.ones([n, 1])

    #计算一般有向图的转移矩阵
    E = np.ones([n, n])
    A = d * transition_matrix + (1 - d) / n * E

    #迭代并规范化
    while True:
        y = np.matmul(A, x0)
        x1 = y / np.linalg.norm(y, ord=np.inf, axis=0, keepdims=False)[0]
        e1 = np.linalg.norm(x1 - x0, ord=2, axis=0, keepdims=False)[0]
        x0 = x1
        if e1 < e:
            break
    print(x0)
if __name__ == "__main__":
    transition_matrix = np.array([[0, 0, 1],
                                  [1/2, 0, 0],
                                  [1/2, 1, 0]])
    d = 0.85
    e = 0.000
    n = 3
    power_method(transition_matrix, d, e, n)